Java Database Programming with JDBC Java Database Programming with JDBC
by Pratik Patel
Coriolis, The Coriolis Group
ISBN: 1576100561   Pub Date: 10/01/96
  

Previous Table of Contents Next


The BuildDB application connects to the SimpleText JDBC driver, creates the ICONCATEGORY table, adds some image category records, creates the ICONSTORE table, and adds some image records. Note that when the image records are added to the ICONSTORE table, a PreparedStatement object is used. We’ll take a closer look at PreparedStatements in Chapter 11; for now, just realize that this is an efficient way to execute the same SQL statement multiple times with different parameters values. Also note that the image data is coming out of GIF files stored on disk. An InputStream is created using these files, which is then passed to the JDBC driver for input. The JDBC driver reads the InputStream and stores the binary data in the database table. Simple, isn’t it? Now that we’ve created the database, we can start writing our IconStore application.

Application Essentials

The source code for the IconStore application is shown throughout the rest of this chapter, broken across the various sections. As always, you can pick up a complete copy of the source code on the CD-ROM. Remember, you need to have the SimpleText JDBC driver installed before using the IconStore application. See Chapter 3, if you have trouble getting the application to run.

Writing The main Method

Every JDBC application must have an entry point, or a place at which to start execution. This entry point is the main method, which is shown in Listing 8.2. For the IconStore application, main simply processes any command line arguments, creates a new instance of the IconStore class (which extends Frame, a top-level window class), and sets up the window attributes. The IconStore application accepts one command line argument: the location of the IconStore database. The default location is /IconStore.

Listing 8.2 IconStore main method.

import java.awt.*;
import java.io.*;
import java.util.*;
import java.sql.*;

public class IconStore
        extends Frame
{
    IconCanvas      imageCanvas;
     List            iconList;
     Panel           iconListPanel;
    MenuBar         menuBar;
     Menu            fileMenu;
    Menu            sectionMenu;
     List            lists[];

     static String myHome = "/IconStore";
    Connection      connection;
    Hashtable       categories;
    Hashtable       iconDesc[];
    String          currentList;
    String          currentFile = null;
     FileDialog      fileDialog;
    //————————————————————————————————————
    // main
    //————————————————————————————————————
    public static void main (String[] args) {

        // If an argument was given, assume it is the location of the
      // database.
        if (args.length > 0) {
            myHome = args[0].trim();

            // If there is a trailing separator, remove it
            if (myHome.endsWith("/") ||
                myHome.endsWith("\\")) {
                myHome = myHome.substring(0, myHome.length() - 1);
            }
        }

        // Create our IconStore object
        IconStore frame = new IconStore();

        // Setup and display
         frame.setTitle("The IconStore");
        frame.init();

        frame.pack();
         frame.resize(300, 400);
        frame.show();
    }

A lot of work is being performed in IconStore.init, such as establishing the database connection, reading the icon categories, creating the menus, and reading the icon descriptions. We’ll take a look at each of these in greater detail in the following sections.

Establishing The Database Connection

Listing 8.3 shows the code used by the IconStore application to connect to the SimpleText JDBC driver.

Listing 8.3 Establishing the database connection.

public Connection establishConnection()
    {

         Connection con = null;
         try {
              // Create an instance of the driver
                 java.sql.Driver d = (java.sql.Driver) Class.forName (
                          "jdbc.SimpleText.SimpleTextDriver").newInstance();

             // Properties for the driver
                java.util.Properties prop = new java.util.Properties();

          // URL to use to connect
             String url = "jdbc:SimpleText";

             // Set the location of the database tables
              prop.put("Directory", myHome);

             // Connect to the SimpleText driver
              con = DriverManager.getConnection(url, prop);
    }
    catch (SQLException ex) {

         // An SQLException was generated. Dump the exception
             // contents. Note that there may be multiple SQLExceptions
             // chainedtogether.

             System.out.println("\n*** SQLException caught ***\n");
            while (ex != null) {
                   System.out.println("SQLState: " + ex.getSQLState());
                 System.out.println("Message:  " + ex.getMessage());
                 System.out.println("Vendor:   " + ex.getErrorCode());
               ex = ex.getNextException();
         }
            System.exit(1);
    }
       catch (java.lang.Exception ex) {
            ex.printStackTrace();
           System.exit(1);
    }
      return con;
}

Note that we need to set a property for the SimpleText driver to specify the location of the database tables. In reality, the SimpleText driver stores each database table as a file, and the Directory property specifies the directory in which these files are kept. As I mentioned in the previous section, the default location is /IconStore (the IconStore directory of your current drive), but this can be overridden to be any location.

If successful, a JDBC Connection object is returned to the caller. If there is any reason a database connection cannot be established, the pertinent information will be displayed and the application will be terminated.


Previous Table of Contents Next